package com.ruoyi.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.api.entity.Message;
import com.ruoyi.api.service.VimUnCountService;
import com.ruoyi.api.vo.UnCount;
import com.ruoyi.api.vo.UnMessage;
import com.ruoyi.api.vo.Users;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.vim.mapper.ImUnCountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class VimUnCountServiceImpl implements VimUnCountService {

    @Autowired
    private ImUnCountMapper imUnCountMapper;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private RedisCache redisCache;

    @Override
    public UnCount select(String fromId, String chatId) {
        UnCount unCount = imUnCountMapper.select(Long.valueOf(fromId),Long.valueOf(chatId));
        return unCount;
    }

    @Override
    public void insert(UnCount counts) {
        imUnCountMapper.inserts(counts);
    }

    @Override
    public void update(UnCount counts) {
        imUnCountMapper.updates(counts);
    }

    @Override
    public UnMessage selects(String userId, String aLong) {
        UnMessage unMessage = new UnMessage();
        String a = redisCache.getCacheObject("count:" +  aLong + "_" + userId);
        Set<ZSetOperations.TypedTuple<String>> sets = redisTemplate.opsForZSet().reverseRangeWithScores("unread-message" + "-" + aLong, 0, 0);
        if(sets.size() == 0){
            if (Long.parseLong(userId) < Long.parseLong(aLong)) {
                Set<ZSetOperations.TypedTuple<String>> set = redisTemplate.opsForZSet().reverseRangeWithScores("message" + "-" + userId + "-" + aLong, 0, 0 );
                for (ZSetOperations.TypedTuple<String> stringTypedTuple : set) {
                    String b = stringTypedTuple.getValue();
                    Message message = JSON.parseObject(b,Message.class);
                    unMessage.setContent(message.getContent());
                } }else {
                Set<ZSetOperations.TypedTuple<String>> set = redisTemplate.opsForZSet().reverseRangeWithScores("message" + "-" + aLong + "-" + userId, 0, 0);
                for (ZSetOperations.TypedTuple<String> stringTypedTuple : set) {
                    String b = stringTypedTuple.getValue();
                    Message message = JSON.parseObject(b,Message.class);
                    unMessage.setContent(message.getContent());
                }
            }
        }else {
//            Set<ZSetOperations.TypedTuple<String>> set = redisTemplate.opsForZSet().reverseRangeWithScores("unread-message" + "-" + aLong, 0, 0);
            for (ZSetOperations.TypedTuple<String> stringTypedTuple : sets) {
                String b = stringTypedTuple.getValue();
                Message message = JSON.parseObject(b, Message.class);
                unMessage.setContent(message.getContent());
            }
        }
        Users user = imUnCountMapper.selects(Long.valueOf(aLong));
        unMessage.setUsers(user);
        unMessage.setUncount(a);
        return unMessage;
    }

}
